﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

    public partial class Default : System.Web.UI.MasterPage
    {

        protected void Page_PreRender(object sender, EventArgs e)
        {
           
            if (Common.GetLoggedUser(this.Page) == null)//pro neprihlaseneho se zobrazi panel pro prihlaseni
                pnlNotLogged.Visible = true;
            else
            {
                //pokud je prihlaseny panel se skryje
                pnlNotLogged.Visible = false;
                LinkButtonNick.Text = Common.GetLoggedUser(this.Page).Nick;

                //obsah praveho sloupce
                SetRightCollumn();

                SetRepeaters();

                LinkButton6.Visible = IsAnyLeader();

                LinkButtonAdministration.Visible = MidApp.ManageIsAdministrator.IsAdministrator(Common.GetLoggedUser(this.Page).UserID);
                LinkButton8.Visible = IsNotAdmin();
                LinkButton7.Visible = IsNotAdmin();
                LinkButton10.Visible = IsNotAdmin();
                LinkButton11.Visible = IsNotAdmin();
                LinkButton6.Visible = IsNotAdmin();
                LinkButton9.Visible = IsNotAdmin();
            }

            pnlLogged.Visible=!pnlNotLogged.Visible;//viditelnost tohoto druheho panelu je vzdy opacna
        }

        private void SetRightCollumn()
        {
            lblUserNick2.Text = Common.GetLoggedUser(this.Page).Nick;
            lblUserName.Text = Common.GetLoggedUser(this.Page).Name + " " + Common.GetLoggedUser(this.Page).Surname;
            lblUserAddress.Text = Common.GetLoggedUser(this.Page).Address;
            lblUserMail.Text = Common.GetLoggedUser(this.Page).PrimaryEmail;
            lblUserPhone.Text = (Common.GetLoggedUser(this.Page).PhoneNumber == "" ? "" : "Tel.: ") + Common.GetLoggedUser(this.Page).PhoneNumber;
            lblUserICQ.Text = (Common.GetLoggedUser(this.Page).ICQNumber == "" ? "" : "ICQ: ") + Common.GetLoggedUser(this.Page).ICQNumber;
            userGravatar.Email = Common.GetLoggedUser(this.Page).PrimaryEmail;
            userGravatar.DefaultImage = MidApp.ManageUser.GetUserImage(Common.GetLoggedUser(this.Page).UserID);
        }

        private void SetRepeaters()
        {
            RepeaterTeamLeader.DataSource = MidApp.ManageTeam.GetTeamsWhereUserIsLeader(Common.GetLoggedUser(Page).UserID);
            RepeaterTeamLeader.DataBind();

            RepeaterTeamMember.DataSource = MidApp.ManageTeam.GetTeamsWhereUserIsMember(Common.GetLoggedUser(Page).UserID);
            RepeaterTeamMember.DataBind();

            RepeaterTeamTreasurer.DataSource = MidApp.ManageTeam.GetTeamsWhereUserIsTreasurer(Common.GetLoggedUser(Page).UserID);
            RepeaterTeamTreasurer.DataBind();
        }

        protected void LinkButtonTeam_Click(object sender, EventArgs e)
        {
            Response.Cookies["teamID"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("EditTeam.aspx");
        }
        protected void LinkButtomDeleteTeam_Click(object sender, EventArgs e)
        {
            MidApp.ManageTeam.DeactivateTeam(int.Parse(((LinkButton)sender).CommandArgument));
            Response.Redirect("default.aspx");
        }
        //metoda vrati ID teamu, ID tymu je nasledne ulozeno do command argumentu LinkButtonTeam
        protected int AssignTeamID(object sender)
        {
            int aux = ((AppLib.Team)sender).TeamID;
            return aux;
        }
        protected string AssignTeamAvatar(object sender)
        {
            string aux = ((AppLib.Team)sender).ImageURL;
            return aux;
        }
        //obsluha udalosti stisku tlacitka pro odchod z tymu
        protected void LinkButtonLeaveTeam_Click(object sender, EventArgs e)
        {
            var aux = ((LinkButton)sender).CommandArgument;
            var team = int.Parse(aux);
            MidApp.ManageIsMember.DisApproveUserToTeam(Common.GetLoggedUser(this.Page).UserID, team);
            Response.Redirect("Default.aspx");

        }
        protected string GetMemberCredit(object sender)
        {
            var team = ((AppLib.Team)sender);
            return MidApp.ManageStock.GetPlayer(team.TeamID, Common.GetLoggedUser(this.Page).UserID).Credit.ToString();
        }
        //zjistu jestli, je nekde uzivatel vedouci, aby se mohlo zobrazit tlacitko pro vypis akci
        protected bool IsAnyLeader()
        {
            return MidApp.ManageIsMember.IsLeaderToAnyTeam(Common.GetLoggedUser(this.Page).UserID);
        }
        protected bool IsNotAdmin()
        {
            return !(MidApp.ManageIsAdministrator.IsAdministrator(Common.GetLoggedUser(this.Page).UserID));
        }

        //klik tlacitka presmeruje na detail o zalogovanem uzivateli
        protected void LinkButtonNick_Click(object sender, EventArgs e)
        {
            Response.Cookies["UserDetail"].Value = Common.GetLoggedUser(this.Page).UserID.ToString();
            Response.Redirect("UserDetail.aspx");
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            Common.SetLoggedUser(this.Page, null);
            Response.Redirect("Login.aspx");
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            Response.Cookies["EventAdmin"].Value = "-1";
            Response.Cookies["State"].Value = "1";
            Response.Redirect("EventsList.aspx");
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            Response.Redirect("Registration.aspx");
        }

        protected void LinkButton5_Click(object sender, EventArgs e)
        {
            Response.Redirect("Edit.aspx");
        }

        protected void LinkButton6_Click(object sender, EventArgs e)
        {
            Response.Redirect("NewEvent.aspx");
        }

        protected void LinkButton7_Click(object sender, EventArgs e)
        {
            Response.Redirect("NewTeam.aspx");
        }
        protected void LinkButton8_Click(object sender, EventArgs e)
        {
            Response.Cookies["FindingState"].Value = "-1";
            Response.Redirect("TeamList.aspx");          
        }
        protected void LinkButtonTeamDetails_Click(object sender, EventArgs e)
        {
            Response.Cookies["TeamDetail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("TeamDetails.aspx");
        }
        protected void LinkButtonTeamInvite_Click(object sender, EventArgs e)
        {
            Response.Cookies["TeamID"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("InviteIntoTeam.aspx");
        }
        protected void LinkButtonSendMail_Click(object sender, EventArgs e)
        {
            Response.Cookies["TeamMail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("SendMail.aspx");
        }
        protected void LinkbuttonTransaction_Click(object sender, EventArgs e)
        {
            Response.Cookies["Transaction"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("Transaction.aspx");
        }
        protected void LinkButtonEventStats_Click(object sender, EventArgs e)
        {
            Response.Cookies["EventStats"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("EventStatistics.aspx");
        }
        protected void LinkButtonFinStats_Click(object sender, EventArgs e) {

            Response.Cookies["TeamFin"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("TeamStatistics.aspx");
        }
        protected void LinkButton9_Click(object sender, EventArgs e)
        {
            Response.Redirect("ForumList.aspx");
        }

        protected void LinkButton10_Click(object sender, EventArgs e)
        {
            Response.Redirect("ChargeRequest.aspx");
        }

        protected void LinkButton11_Click(object sender, EventArgs e)
        {
            Response.Redirect("Administration.aspx");
        }

        protected void LinkButton11_Click1(object sender, EventArgs e)
        {
            Response.Redirect("Help.aspx");
        }
        protected void LinkButton12_Click(object sender, EventArgs e)
        {
            Response.Redirect("HelpRegister.aspx");
        }
    }
